// pages/travelAgency/locate.js
var Tool = require("../../utils/ztool.js");
var QQMapWX = require('../../utils/qqmap-wx-jssdk.js');
var tool = new Tool();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    list: [],
    IDD: '',
    retype: '',
    DriverID: '',

    latitude: '',//31.3010678543
    longitude: '',  //120.5811424828
    latitudewx: '',
    longitudewx: '',

  },

  getLocation: function () {

    var that = this

    wx.getLocation({
      type: 'wgs84',
      success: function (res) {
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude
        })
      }
    })
    console.log(that.data.longitude, that.data.latitude)
  },

  showError: function (msg) {
    var that = this;
    that.setData({
      formError: true,
      errorMessage: msg
    });

    setTimeout(function () {
      that.setData({
        formError: false,
        errorMessage: '未知错误'
      });
    }, 2000);
  },




  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    // 页面初始化 options为页面跳转所带来的参数
    var IDD1 = options.IDD
    var DriverID = options.DriverID
    var retype = options.retype
    that.setData({
      IDD: IDD1,
      DriverID: DriverID,
      retype: retype
    });
    getLocaData(this)
    console.log(that.data.DriverID, that.data.retype)

  },




  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})

// IDD: '',
// retype: '',
// DriverID: '',
function getLocaData(that) {
  that.data.tag = ''
  var userInfo = wx.getStorageSync('userInfo')
  console.log(userInfo)
  wx.request({
    url: 'https://www.lanshuicar.com/Server/CarCompany/CarCompMap.ashx',
    data: {
      idd: that.data.IDD,
      rtype: userInfo.USerType,
      retype: that.data.retype,
      id: that.data.DriverID

    },
    method: 'GET',
    success: function (res) {
      // success
      var data = tool.getRegularJson(res.data);
      var code = data.code;
      if (code == 0) {
        that.setData({
          list: data.data[0],

        })
        that.setData({
          latitude: that.data.list.mapW,
          longitude: that.data.list.mapJ,
        })
        console.log(that.data.list)

        //转换坐标
        if (that.data.latitude == '4.9E-324' || that.data.longitude == '4.9E-324') {
          that.setData({
            latitudewx: 31.298886,
            longitudewx: 120.58531600000003,
            markers: [{
              iconPath: "../../images/order_list/locate.png",
              id: 0,
              latitude: 31.298886,
              longitude: 120.58531600000003,
              width: 30,
              height: 30,
              callout: {
                content: '尚未获取到位置，请相应的人员重新进行定位',
                color: '#fff',
                fontSize: 17,
                bgColor: '#7abcf6',
                display: 'ALWAYS',
                borderRadius: 8,
                padding: 5,

              },

            }],
          })
          return
        } else {
          reversQQlocate(that)
        }

      } else if (code == -1) {
        // that.showError('')
        console.log(data)
        that.setData({
          latitudewx: 31.298886,
          longitudewx: 120.58531600000003,
          markers: [{
            iconPath: "../../images/order_list/locate.png",
            id: 0,
            latitude: 31.298886,
            longitude: 120.58531600000003,
            width: 30,
            height: 30,
            callout: {
              content: '当前人员尚未开启GPS功能',
              color: '#fff',
              fontSize: 17,
              bgColor: '#7abcf6',
              display: 'ALWAYS',
              borderRadius: 8,
              padding: 5,

            },

          }],
        })
      }
    },
    fail: function () {
      console.log('fail-1')
      // fail
    }
  })
}

function reversQQlocate(that) {
  console.log(that.data.retype)
  var conte = ''
  if (that.data.retype == 3) {
    conte = that.data.list.sCarNum + '\n' + that.data.list.sName + '\n' + that.data.list.sTel
  } else if (that.data.retype == 4 || that.data.retype == 5) {
    conte = that.data.list.sName + '\n' + that.data.list.sTel
  }
  var demo = new QQMapWX({
    key: 'ORIBZ-4SMCD-UQQ4D-HCT67-FBNLK-3FBMO'
  });
  demo.reverseGeocoder({
    location: {
      latitude: that.data.latitude,
      longitude: that.data.longitude
    },
    coord_type: 3,//baidu经纬度
    success: function (res) {
      console.log(res)
      var location = res.result.ad_info.location;
      console.log(location.lat)
      that.setData({
        latitudewx: location.lat,
        longitudewx: location.lng,
        markers: [{
          iconPath: "../../images/order_list/locate.png",
          id: 0,
          latitude: location.lat,
          longitude: location.lng,
          width: 30,
          height: 30,
          callout: {
            content: conte,
            color: '#fff',
            fontSize: 17,
            bgColor: '#7abcf6',
            display: 'ALWAYS',
            borderRadius: 8,
            padding: 5,

          },

        }],
        circles: [{
          latitude: location.lat,
          longitude: location.lng,
          radius: 10,
          fillColor: '#7cb5ec88',
          color: '#7cb5ec88'

        }]
      })
      console.log(that.data.latitudewx)
    },
    fail: function (res) {
      console.log(res);
      console.log('11')
      that.showError('未获取到目标位置,请重新进行定位')
    },
    complete: function (res) {
      console.log(res);
    }
  });
  console.log(that.data.latitudewx)
}